/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modelo.dao;

/**
 *
 * @author Rodrigo
 */

import modelo.entidades.*;
import java.util.*;
import java.text.*;
import org.hibernate.Criteria; 
import org.hibernate.criterion.*; 
import org.hibernate.Session; 
import org.hibernate.util.CalendarComparator;

public class UsuarioDAO extends GenericDAO<Usuario> {
    
    public List<Calendar> findAllDistinctData()throws DAOException
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        
        if(!session.getTransaction().isActive())
            session.beginTransaction();
        
        try
        {
            Criteria criteria = session.createCriteria(Usuario.class)
                    .add(Restrictions.isNotNull("dataNascimento"))
                    .setProjection(Projections.distinct(Projections.property("dataNascimento")));
            
            List<Calendar> calendars = criteria.list();
            
            return calendars;
            
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw new DAOException();
        }
        
    }
    
    public List<String> findAllDistinctNome()throws DAOException
    {
        
        Session session = HibernateUtil.getSessionFactory().openSession();
        
        if(!session.getTransaction().isActive())
            session.beginTransaction();
        
        try
        {
            Criteria criteria = session.createCriteria(Usuario.class)
                    .setProjection(Projections.distinct(Projections.property("nome")));
                    
            
            List<String> usuarios = criteria.list();
            
            session.getTransaction().commit();
            
            return usuarios;
            
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw new DAOException();
        }
        
    }
    
    public Usuario findByLogin(String login)
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        
        if(!session.getTransaction().isActive())
            session.beginTransaction();
        
        Criteria criteria = session.createCriteria(Usuario.class)
                .add(Restrictions.eq("login", login)).setMaxResults(1);
        
        List<Usuario> lista = criteria.list();
        
        if(!lista.isEmpty())
            return lista.get(0);
        else
        return null; 
    }
    
    public Usuario findByLoginAndSenha(String login, String senha)throws DAOException
    {
         Session session = HibernateUtil.getSessionFactory().openSession(); 
        
        if(!session.getTransaction().isActive())
            session.beginTransaction();
        
        Criteria criteria = session.createCriteria(Usuario.class)
                .add(Restrictions.and(Restrictions.eq("login", login),Restrictions.eq("senha", senha)))
                .setMaxResults(1);
        
        List<Usuario> lista = criteria.list();
        
        if(!lista.isEmpty())
            return lista.get(0);
        else    
        return null; 
    }
    
    // dwr nao aceita sobrecarga de metodos 
    
    public List<Usuario> findByFiltro2(String nome, String cargoNome, String data, String login)
            throws DAOException
    {
   
        
        if(data.indexOf("%")!=-1)
        {

            return findByFiltro(nome,cargoNome,null,login);
        }
           

        
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        
        try
        {
        Date date = (Date) format.parse(data);
        
 
       Calendar calendar = Calendar.getInstance();
       calendar.setTime(date);
        
      return findByFiltro(nome,cargoNome,calendar,login);
        
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw new DAOException();
        }
        
    }
    
    public List<Usuario> findByFiltro(String nome, String cargoNome, Calendar data, String login)
            throws DAOException
    {
        
         Session session = HibernateUtil.getSessionFactory().openSession(); 
        
        if(!session.getTransaction().isActive())
            session.beginTransaction();
        
        Criteria criteria = session.createCriteria(Usuario.class)
               .add(Restrictions.conjunction()
                .add(Restrictions.ilike("nome",nome))
                .add(Restrictions.ilike("login",login))
                 
                ).createAlias("cargo","c").add(Restrictions.ilike("c.nome",cargoNome));

        
           if(data!=null)
           {
               criteria.add(Restrictions.eq("dataNascimento", data));
           }
                
        
        List<Usuario> lista = criteria.list();
        
        return lista; 
    }
    
    public List<Usuario> findAll()throws DAOException
    {

        return super.findAll(Usuario.class);      
    }
    
    public Usuario find(Integer id)throws DAOException
    {

        return super.find(id,Usuario.class);      
    }
    
}
